﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using System.Reflection;
using System.Data;
using QuantitativeInvestment.Tools;
namespace QuantitativeInvestment.Model
{
     class Model
    {
        public string name;
        public Dictionary<string, Parameter> paraList;
        public List<Factor.Factor> factorList;
        public DataModel dataModel;
        public Container.Container container;
        public Model()
        {
            this.paraList = new Dictionary<string, Parameter>();
            this.factorList = new List<Factor.Factor>();
            this.dataModel = new DataModel();
            this.factorList.Add(Factor.Factor.createFactorByName("价格"));
        }
        public virtual List<TradingStock> buy(List<TradingStock> stockList,string tradingDate)
        {

            return null;
        }

        public virtual List<TradingStock> sell(List<TradingStock> stockList,string tradingDate)
        {
            return null;
        }

        //根据关键字来进行，排序
        public List<TradingStock> sort(List<TradingStock> tradingStocks,string sortType="低")
        {
            for (int i = 0; i < tradingStocks.Count; i++)
            {
                for (int j = 0; j < tradingStocks.Count - i - 1; j++)
                {
                    if (tradingStocks[j].comparedValue > tradingStocks[j + 1].comparedValue)
                    {
                        TradingStock tempStock = tradingStocks[j];
                        tradingStocks[j] = tradingStocks[j + 1];
                        tradingStocks[j + 1] = tempStock;
                    }
                }
            }

            if (sortType == "高")
            {
                List<TradingStock> newTradingStocks = new List<TradingStock>();
                for (int i = tradingStocks.Count-1; i >= 0; i--)
                {
                    newTradingStocks.Add(tradingStocks[i]);
                }
                return newTradingStocks;

            }


            return tradingStocks;
        }
        public static Model createModelByName(String name)
        {
            Model model = null;
            DataSet ds = new DataSet();
            ds.ReadXml(System.Windows.Forms.Application.StartupPath + "/Config" + "/Models.xml");
            DataRow[] rows = ds.Tables["model"].Select("name='" + name + "'");
            if (rows.Length > 0)
            {
                Assembly a = Assembly.GetExecutingAssembly();
                model = (Model)a.CreateInstance(rows[0]["objectName"].ToString());
            }

            return model;
        }

        public static List<string> getModelNameList()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(System.Windows.Forms.Application.StartupPath + "/Config"+"/Models.xml");
            List<string> modelNameList = new List<string>();
            foreach (DataRow row in ds.Tables["model"].Rows)
            {
                modelNameList.Add(row["name"].ToString());
            }

            return modelNameList;
        }


    }

    
}
